using System.Text;
using System.Text.Json;

namespace FastDevTemplate.Common.Util;

public class HttpRequester
{
    public static async Task<T?> GetAsync<T>(string url)
    {
        using var client = new HttpClient();
        var response = await client.GetAsync(url);
        var content = await response.Content.ReadAsStringAsync();
        return JsonSerializer.Deserialize<T>(content);
    }

    public static async Task<T?> PostAsync<T>(string url, object data)
    {
        using var client = new HttpClient();
        var json = JsonSerializer.Serialize(data);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        var response = await client.PostAsync(url, content);
        var result = await response.Content.ReadAsStringAsync();
        return JsonSerializer.Deserialize<T>(result);
    }
    
}